﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Ketnoidulieu
{
    public partial class frmTonghopTreeList : Form
    {
        public frmTonghopTreeList()
        {
            InitializeComponent();
        }

        clssQLDiem obj = new clssQLDiem();

        private void frmTonghopTreeList_Load(object sender, EventArgs e)
        {
            DataSet dsLop = new DataSet();
            DataSet dsSV = new DataSet();
            dsLop = obj.laydulieu();
            dsSV = obj.laydulieu("select * from sinhvien");
            hienthidanhsachlop(dsLop, dsSV, tvw1);
        }

        void hienthidanhsachlop(DataSet dsl, DataSet dssv, TreeView t)
        {
            tvw1.Nodes.Clear();
            foreach (DataRow drLop in dsl.Tables[0].Rows)
            {
                TreeNode nodecha = t.Nodes.Add(drLop["tenlop"].ToString());
                nodecha.Tag = drLop["malop"].ToString();
                foreach (DataRow drSV in dssv.Tables[0].Rows)
                {
                    if (drSV["malop"].ToString() == drLop["malop"].ToString())
                    {
                        TreeNode nodecon = nodecha.Nodes.Add(drSV["tensv"].ToString());
                        nodecon.Tag = drSV["masv"].ToString();
                    }
                        
                }

            }
        }

        void taocottheosv()
        {
            lvw1.Columns.Clear();
            lvw1.Columns.Add("MaSV",80);
            lvw1.Columns.Add("TenSV",106);
            lvw1.Columns.Add("DiaChi",200);
            lvw1.View=View.Details;
            lvw1.FullRowSelect=true;
            lvw1.MultiSelect=false;
        }

        void taocottheodiemsv()
        {
            lvw1.Columns.Clear();
            lvw1.Columns.Add("TenMH");
            lvw1.Columns.Add("DiemMH");
            lvw1.Columns.Add("LanThi");
            lvw1.Columns.Add("SoTinChi");
            lvw1.View = View.Details;
            lvw1.FullRowSelect = true;
            lvw1.MultiSelect = false;
        }

        void hienthidanhsachsinhvientheolop(string malop,ListView l)
        {
            string sql = "select * from sinhvien where malop='" + malop + "'";
            DataSet dsSV = obj.laydulieu(sql);
            taocottheosv();
            l.Items.Clear();
            foreach (DataRow dr in dsSV.Tables[0].Rows)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = dr["masv"].ToString();
                lvi.SubItems.Add(dr["tensv"].ToString());
                lvi.SubItems.Add(dr["diachi"].ToString());
                l.Items.Add(lvi);
            }
        }

        void hienthidiemsinhvien(string masv, ListView l)
        {
            string sql = "select tenmh,diem,lanthi,sotc from monhoc,diem where monhoc.mamh=diem.mamh and masv='" + masv + "'";
            DataSet dsSV = obj.laydulieu(sql);
            taocottheodiemsv();
            l.Items.Clear();
            foreach (DataRow dr in dsSV.Tables[0].Rows)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = dr["tenmh"].ToString();
                lvi.SubItems.Add(dr["diem"].ToString());
                lvi.SubItems.Add(dr["lanthi"].ToString());
                lvi.SubItems.Add(dr["sotc"].ToString());
                l.Items.Add(lvi);
            }
        }

        private void tvw1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (tvw1.SelectedNode != null)
            {
                if (tvw1.SelectedNode.Level == 0)
                {
                    gpb1.Text = "Danh sách sinh viên";
                    hienthidanhsachsinhvientheolop(tvw1.SelectedNode.Tag.ToString(), lvw1);
                }
                else
                {
                    gpb1.Text = "Điểm của sinh viên";
                    hienthidiemsinhvien(tvw1.SelectedNode.Tag.ToString(), lvw1);
                }
            }
        }
    }
}
